var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

var moreData = "";
function getAllDCdateBuType() {
    $.ajax({
        url: root.logistics.getPageWarehouseCoverAddress,
        cache: false, //禁用缓存
        type: "POST",
        success: function(result){
            if(result.errorCode=="0"){
                moreData = result.data.result;
                goodsList(result);
            }else {
                layer.msg(result.errorMessage, {icon: 5,time:1000});
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function goodsList(dataList){
    console.log(dataList)
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }

    if(dataList.data != null){
        $.each(dataList.data.result, function(i, field) {
            var rowData = new Array();
            rowData.push(field.warehouse_name);
            rowData.push(field.warehouse_cover_address);
            rowData.push("有效");
            rowData.push('<a onclick="updataDcCity(\''+field.warehouse_name+'\')" class="powerButton 132">修改</a>')
            dataSource.push(rowData);
            rowData.push(field.warehouse_code);

        });
    }

    rangeDataTable(dataSource);
    function rangeDataTable(dataSource) {
        var dttable = $('#warehouseTable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#warehouseTable").DataTable({
            "autoWidth": false,
            "ordering": true,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function(data, type, row, meta) {
                        var dataAll = row[1];
                        var ulTop = '<ul class="radius text-l" id="ulList">';
                        var list="";
                        var ulfoot ='</ul><a class="edit powerButton 131" style="float: right;" onclick="area(\'修改覆盖范围\',this,\''+row[5]+'\',\''+row[0]+'\')">编辑</a>';
                        for(var i=0;i<dataAll.length;i++) {
                            if (dataAll[i].child_address_library.length == 0){
                                list += '<li class="f-l ml-10 liList" id="' + dataAll[i].code + '"><span class="name">' + dataAll[i].name + '</span><sup style="color:#ff1b1b; cursor:pointer;">' +
                                    '<b onclick="remove(this,' + row[5] + ',' + row[0] + ')">x</b></sup></li>'
                            }else{
                                list += '<li class="f-l ml-10 liList" id="' + dataAll[i].code + '"><span class="name">' + dataAll[i].name + '<a id="'+dataAll[i].code+'" class="powerButton 130" onclick="more(this,'+row[4]+')"> 更多</a></span><sup style="color:#ff1b1b; cursor:pointer;">' +
                                    '<b onclick="remove(this,' + row[5] + ',' + row[0] + ')">x</b></sup></li>'
                            }

                        }
                        ulTop += list + ulfoot;
                        return ulTop;
                    },
                    "targets": 1
                },

                /*
                {
                    "render": function(data, type, row, meta) { // 1,6
                        var idList =row[6].split(",");
                        var nameList = row[1].split(",");
                        var ulTop = '<ul class="radius text-l" id="ulList">';
                        var list="";
                        var ulfoot ='</ul><a class="edit" style="float: right;" onclick="area(\'修改覆盖范围\',this,'+row[5]+','+row[0]+')">编辑</a>';
                        for(var i=0;i<idList.length;i++){
                            list += '<li class="f-l ml-10 liList" id="'+idList[i]+'"><span class="name">'+nameList[i]+'</span><sup style="color:#ff1b1b; cursor:pointer;">' +
                                '<b onclick="remove(this,'+row[5]+','+row[0]+')">x</b></sup></li>'
                        }

                        ulTop += list + ulfoot;
                        return ulTop;
                    },
                    "targets": 1
                },

                {
                    "render": function(data, type, row, meta) {
                        if(row[2] == 10){
                            return '有效';
                        }else{
                            return '无效';
                        }
                    },
                    "targets": 2
                },

                {
                    "render": function(data, type, row, meta) {
                        var val = '<a onclick="relation_update(\'修改覆盖范围\','+row[0]+')"><span id="areaList" class="list">修改</span></a>';
                        return val;
                    },
                    "targets": 5
                },*/
            ]
        });
        //权限控制函数
        power()
    };
}

function more(thil,warhouseCode) {
    var allCity = "";
    for(var i=0;i<moreData.length;i++) {
        if (warhouseCode == moreData[i].id){
            var cityList = moreData[i].warehouse_cover_address;
            for(var j=0;j<cityList.length;j++) {
                if (thil.id == cityList[j].code){
                    allCity = cityList[j].child_address_library;
                }
            }


        }
    }
    var cityName = "";
    for(var i=0;i<allCity.length;i++) {
        if(allCity[i].child_address_library.length==0){
            cityName +=  allCity[i].name+'全境,'
        }else {
            for(var k=0;k<allCity[i].child_address_library.length;k++) {

                cityName += allCity[i].name + allCity[i].child_address_library[k].name+','
            }
        }

    }
    layer.confirm(cityName, {
        btn: ['返回'] //按钮
    }, function(index,layero){
        layer.close(index);
        //确定执行方法
    });
    
}
function updataDcCity(id) {
    alert(id)
}